<?php
/**
$ip = $_SERVER['REMOTE_ADDR'];
if ($ip != '219.89.163.221' &&
    $ip != '60.190.131.94') {
  header( 'Location: ../../maintenance.html');
  return;
}
*/
require_once dirname(__FILE__) . '/common/common.inc.php';

if (isset($_COOKIE[USER]) && isset($_COOKIE[MODEL]) && isset($_COOKIE[STATE]) && isset($_COOKIE[TRANSITION])) {
  $_SESSION[USER] = json_decode($_COOKIE[USER]);
  $_SESSION[MODEL] = json_decode($_COOKIE[MODEL]);
  $_SESSION[STATE] = json_decode($_COOKIE[STATE]);
  $_SESSION[TRANSITION] = json_decode($_COOKIE[TRANSITION]);
}

$template = MiscUtils::getParam('t', 'signin');
$base = MiscUtils::getParam('b', 'base');
$options = (MiscUtils::decryptParam('opts', NULL) == NULL) ? MiscUtils::encode(array()) : $options;
$menu = MiscUtils::decryptParam('m', new stdClass());

$smarty->assign('user', MiscUtils::encode(array()));
$smarty->assign('date', time());
$smarty->assign('options', $options);
$smarty->assign('data', MiscUtils::encode(array()));
$smarty->assign('menu', json_decode($menu));

$logout = MiscUtils::getParam('logout', NULL);
if ($logout === '') {
  unset($_SESSION[USER]);
  unset($_SESSION[MODEL]);
  unset($_SESSION[STATE]);
  unset($_SESSION[TRANSITION]);
  setcookie(USER, '', time() - 1, PATH, DOMAIN);
  setcookie(MODEL, '', time() - 1, PATH, DOMAIN);
  setcookie(STATE, '', time() - 1, PATH, DOMAIN);
  setcookie(TRANSITION, '', time() - 1, PATH, DOMAIN);
  $smarty->assign('template', 'signin');
  $smarty->display("gui/$base.tpl");
  return; /* SignOut */
}

if (isset($_SESSION[USER])) {
  $data = array();
  $data['pz_model'] = $_SESSION[MODEL];
  $data['pz_model_state'] = $_SESSION[STATE];
  $data['pz_model_transition'] = $_SESSION[TRANSITION];
  
  //If login already, skip login page. Will go to home page
  if ($_SESSION[USER]->type == USER_TYPE_ASSISTANT && $template == 'signin') {
    $template = 'passistantall';
  } else if ($_SESSION[USER]->type == USER_TYPE_ADMINISTER && $template == 'signin') {
    $template = 'padminspreadsheet';
  } else if ($_SESSION[USER]->type == USER_TYPE_DESIGNER && $template == 'signin') {
    $template = 'pdesignerall';
  } else if ($_SESSION[USER]->type == USER_TYPE_MAILER && $template == 'signin') {
    $template = 'pmaileraddparcel';
  }
  
  $data['photographer'] = MiscUtils::db(getMyPdo(), '*', 'pz_user', NULL, 't.type = \'' . USER_TYPE_PHOTOGRAPHER . '\'', NULL, NULL, NULL, NULL);
  $data['culture'] = MiscUtils::db(getMyPdo(), '*', 'pz_culture', NULL, NULL, NULL, NULL, NULL, NULL);
  $data['ceremony'] = MiscUtils::db(getMyPdo(), '*', 'pz_ceremony', NULL, NULL, NULL, NULL, NULL, NULL);
  $data['reception'] = MiscUtils::db(getMyPdo(), '*', 'pz_reception', NULL, NULL, NULL, NULL, NULL, NULL);
  $data['color'] = MiscUtils::db(getMyPdo(), '*', 'pz_color', NULL, NULL, NULL, NULL, NULL, NULL);
  $data['designer'] = MiscUtils::db(getMyPdo(), '*', 'pz_user', NULL, 't.type = \'' . USER_TYPE_DESIGNER . '\'', NULL, NULL, NULL, NULL);
  
  setData($template, $data);
  
  $smarty->assign('u', $_SESSION[USER]);
  
  $smarty->assign('template', $template);
  $smarty->assign('user', MiscUtils::encode($_SESSION[USER]));
  $smarty->assign('date', time());
  $smarty->assign('options', urldecode($options));
  $smarty->display("gui/$base.tpl");
  return;
} else {
  $smarty->assign('template', 'signin');
  $smarty->display("gui/$base.tpl");
  return; /* SignIn Required */
}

function setData($template, $data) {
  $task_select = "t.*, 
                  t1.name AS photographer,
                  t2.name AS designer,
                  t3.name AS custom_assignee, 
                  t4.name AS custom_assigner,
                  t5.name AS photographer2";
                  
  
  $task_join = array(array('pz_user', 't.photographer_id = t1.id'),
                     array('pz_user', 't.designer_id = t2.id'),
                     array('pz_user', 't.custom_assignee_id = t3.id'),
                     array('pz_user', 't.custom_assigner_id = t4.id'),
                     array('pz_user', 't.photographer2_id = t5.id')
                    );
  $select = "t.date, t.state_id as s_id, t.transition_id as t_id, t.message, t.overdue, t.task_id,
             t1.*,
             t2.name AS photographer, 
             t3.name AS designer,
             t4.name AS user,
             t5.name AS custom_assignee, 
             t6.name AS custom_assigner,
             t7.name AS photographer2";
  
  $join = array(array('pz_task', 't.task_id = t1.id'),
                array('pz_user', 't1.photographer_id = t2.id'), 
                array('pz_user', 't1.designer_id = t3.id'),
                array('pz_user', 't.user_id = t4.id'),
                array('pz_user', 't1.custom_assignee_id = t5.id'),
                array('pz_user', 't1.custom_assigner_id = t6.id'),
                array('pz_user', 't1.photographer2_id = t7.id')
               );
  
  $smrycond_admin = "(t.is_canceled = 0 AND ((t.state_id = 6 OR t.state_id = 7 OR t.state_id = 10 OR t.state_id = 14 OR t.state_id = 16) OR 
                                             ((t.state_id = 19 OR t.state_id = 21) AND t.custom_assigner_id = " . $_SESSION[USER]->id . ") OR
                                             ((t.state_id = 18 OR t.state_id = 20) AND t.custom_assignee_id =  " . $_SESSION[USER]->id . ")))";
  $smrycond_asst = "(t.is_canceled = 0 AND ((t.state_id = 2 OR t.state_id = 3 OR t.state_id = 4 OR t.state_id = 5 OR t.state_id = 8 OR t.state_id = 11 OR t.state_id = 12 OR t.state_id = 13 OR t.state_id = 16) OR
                                            ((t.state_id = 19 OR t.state_id = 21) AND t.custom_assigner_id = " . $_SESSION[USER]->id . ") OR
                                            ((t.state_id = 18 OR t.state_id = 20) AND t.custom_assignee_id =  " . $_SESSION[USER]->id . ")))";
  $smrycond_des = "(t.is_canceled = 0 AND ((t.state_id = 9 AND t.designer_id = " . $_SESSION[USER]->id . ") OR
                                           ((t.state_id = 19 OR t.state_id = 21) AND t.custom_assigner_id = " . $_SESSION[USER]->id . ") OR
                                           ((t.state_id = 18 OR t.state_id = 20) AND t.custom_assignee_id =  " . $_SESSION[USER]->id . ")))";
  
  switch ($template) {
    case 'busers':
      p_busers($data, $smrycond_admin);
      break;
    case 'buser':
      p_buser($data, $smrycond_admin);
      break;
    case 'bmodels':
      p_bmodels($data, $smrycond_admin);
      break;
    case 'bmodel':
      p_bmodel($data, $smrycond_admin);
      break;
    case 'padminall':
      p_padminall($data, $task_select, $task_join, $select, $join, $smrycond_admin);
      break;
      
    case 'ppassistantbackup':
      p_ppassistantbackup($data, $task_select, $task_join, $select, $join, $smrycond_admin);
      break;
    case 'ppassistantcheck':
      p_ppassistantcheck($data, $task_select, $task_join, $select, $join, $smrycond_admin);
      break;
    case 'ppassistantnocheck':
      p_ppassistantnocheck($data, $task_select, $task_join, $select, $join, $smrycond_admin);
      break;
    case 'ppadminconfirmphoto':
      p_ppadminconfirmphoto($data, $task_select, $task_join, $select, $join, $smrycond_admin);
      break;
    case 'ppassistantpickup':
      p_ppassistantpickup($data, $task_select, $task_join, $select, $join, $smrycond_admin);
      break;
    case 'ppassistantauthor':
      p_ppassistantauthor($data, $task_select, $task_join, $select, $join, $smrycond_admin);
      break;
    case 'ppassistantrauthor':
      p_ppassistantrauthor($data, $task_select, $task_join, $select, $join, $smrycond_admin);
      break;
    case 'ppassistantpublish':
      p_ppassistantpublish($data, $task_select, $task_join, $select, $join, $smrycond_admin);
      break;
    case 'padminconfirmphoto':
      p_padminconfirmphoto($data, $task_select, $task_join, $select, $join, $smrycond_admin);
      break;
    case 'padminnoconfirmphoto':
      p_padminnoconfirmphoto($data, $task_select, $task_join, $select, $join, $smrycond_admin);
      break;
    case 'padminconfirmsort':
      p_padminconfirmsort($data, $task_select, $task_join, $select, $join, $smrycond_admin);
      break;
    case 'padminconfirmpublish':
      p_padminconfirmpublish($data, $task_select, $task_join, $select, $join, $smrycond_admin);
      break;
    case 'padminspreadsheet':
      p_padminspreadsheet($data, $task_select, $task_join, $select, $join, $smrycond_admin);
      break;
    case 'padmindesignertask':
      p_padmindesignertask($data, $task_select, $task_join, $select, $join, $smrycond_admin);
      break;
    case 'padmindesignertaskundo':
      p_padmindesignertaskundo($data, $task_select, $task_join, $select, $join, $smrycond_admin);
      break;
    case 'padminproject':
      p_padminproject($data, $smrycond_admin);
      break;
    case 'padminsheet':
      p_padminsheet($data, $task_select, $task_join, $select, $join, $smrycond_admin);
      break;
    case 'padminscoresheet':
      p_padminscoresheet($data, $smrycond_admin);
      break;
    case 'padminscoresheetdetail':
      p_padminscoresheetdetail($data, $smrycond_admin);
      break;
    case 'padminmailall':
      p_padminmailall($data, $task_select, $task_join, $select, $join, $smrycond_admin);
      break;
    case 'padminpersonal':
      p_padminpersonal($data, $task_select, $task_join, $select, $join, $smrycond_admin);
      break;
    case 'ausers':
      p_ausers($data, $smrycond_asst);
      break;
    case 'auser':
      p_auser($data, $smrycond_asst);
      break;
    case 'passistantall':
      p_passistantall($data, $task_select, $task_join, $select, $join, $smrycond_asst);
      break;
    case 'passistantbackup':
      p_passistantbackup($data, $task_select, $task_join, $select, $join, $smrycond_asst);
      break;
    case 'passistantcheck':
      p_passistantcheck($data, $task_select, $task_join, $select, $join, $smrycond_asst);
      break;
    case 'passistantnocheck':
      p_passistantnocheck($data, $task_select, $task_join, $select, $join, $smrycond_asst);
      break;
    case 'passistantpickup':
      p_passistantpickup($data, $task_select, $task_join, $select, $join, $smrycond_asst);
      break;
    case 'passistantauthor':
      p_passistantauthor($data, $task_select, $task_join, $select, $join, $smrycond_asst);
      break;
    case 'passistantrauthor':
      p_passistantrauthor($data, $task_select, $task_join, $select, $join, $smrycond_asst);
      break;
    case 'passistantpublish':
      p_passistantpublish($data, $task_select, $task_join, $select, $join, $smrycond_asst);
      break;
    case 'passistantproject':
      p_passistantproject($data, $smrycond_asst);
      break;
    case 'passistantmailall':
      p_passistantmailall($data, $task_select, $task_join, $select, $join, $smrycond_asst);
      break;
    case 'passistantpersonal':
      p_passistantpersonal($data, $task_select, $task_join, $select, $join, $smrycond_asst);
      break;
    case 'receivemail':
      receivemail($data, $smrycond_asst, $smrycond_admin);
      break;
    case 'passistantspreadsheet':
      p_passistantspreadsheet($data, $task_select, $task_join, $select, $join, $smrycond_asst);
      break;
    case 'passistantsheet':
      p_passistantsheet($data, $task_select, $task_join, $select, $join, $smrycond_asst);
      break;
    case 'passistantscoresheet':
      p_passistantscoresheet($data, $smrycond_asst);
      break;
    case 'passistantscoresheetdetail':
      p_passistantscoresheetdetail($data, $smrycond_asst);
      break;
    case 'pdesignerall':
      p_pdesignerall($data, $task_select, $task_join, $select, $join, $smrycond_des);
      break;
    case 'pdesignercomplete':
      p_pdesignercomplete($data, $task_select, $task_join, $select, $join, $smrycond_des);
      break;
    case 'pdesignerpersonal':
      p_pdesignerpersonal($data, $task_select, $task_join, $select, $join, $smrycond_des);
      break;
    case 'pdesignerspreadsheet':
      p_pdesignerspreadsheet($data, $task_select, $task_join, $select, $join, $smrycond_des);
      break;
    case 'reviewmail':
      reviewmail($data, $smrycond_admin, $smrycond_asst);
      break;
    case 'bcouriers':
      p_bcouriers($data);
      break;
    case 'bcourier':
      p_bcourier($data);
      break;
    case 'bcultures':
      p_bcultures($data);
      break;
    case 'bculture':
      p_bculture($data);
      break;
    case 'bceremonys':
      p_bceremonys($data);
      break;
    case 'bceremony':
      p_bceremony($data);
      break;
    case 'breceptions':
      p_breceptions($data);
      break;
    case 'breception':
      p_breception($data);
      break;
    case 'bweddingcolors':
      p_bweddingcolors($data);
      break;
    case 'bweddingcolor':
      p_bweddingcolor($data);
      break;
    case 'pmaileraddparcel':
      p_pmaileraddparcel($data);
      break;
    case 'pmailerallparcel':
      p_pmailerallparcel($data, $task_select, $task_join, $select, $join);
      break;
    case 'pmailerunreceivedparcel':
      p_pmailerunreceivedparcel($data, $task_select, $task_join, $select, $join);
      break;
    case 'pmailerreceivedparcel':
      p_pmailerreceivedparcel($data, $task_select, $task_join, $select, $join);
      break;
    case 'pmailerunreceivedcustomer':
      p_pmailerunreceivedcustomer($data, $task_select, $task_join, $select, $join);
      break;
    case 'pmailersheet':
      p_pmailersheet($data, $task_select, $task_join, $select, $join);
      break;
    case 'pmailerscoresheet':
      p_pmailerscoresheet($data);
      break;
    case 'pmailerscoresheetdetail':
      p_pmailerscoresheetdetail($data);
      break;
    case 'plastupdated':
      plastupdated($data);
      break;
    case 'cpassistantbackup':
      p_cpassistantbackup($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin);
      break;
    case 'cpassistantcheck':
      p_cpassistantcheck($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin);
      break;
    case 'cpassistantnocheck':
      p_cpassistantnocheck($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin);
      break;
    case 'cpadminconfirmphoto':
      p_cpadminconfirmphoto($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin);
      break;
    case 'cpadminnoconfirmphoto':
      p_cpadminnoconfirmphoto($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin);
      break;
    case 'cpassistantpickup':
      p_cpassistantpickup($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin);
      break;
    case 'cpdesignercomplete':
      p_cpdesignercomplete($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin);
      break;
    case 'cpadminconfirmsort':
      p_cpadminconfirmsort($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin);
      break;
    case 'cpassistantauthor':
      p_cpassistantauthor($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin);
      break;
    case 'cpassistantrauthor':
      p_cpassistantrauthor($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin);
      break;
    case 'cpassistantpublish':
      p_cpassistantpublish($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin);
      break;
    case 'cpadminconfirmpublish':
      p_cpadminconfirmpublish($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin);
      break;
    case 'cpadminconfirmpublished':
      p_cpadminconfirmpublished($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin);
      break;
    case 'cpmailerunreceivedparcel':
      p_cpmailerunreceivedparcel($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin);
      break;
    case 'cpmailerreceivedparcel':
      p_cpmailerreceivedparcel($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin);
      break;
    case 'cpadminpersonal':
      p_cpadminpersonal($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin);
      break;
    default:
      break;
  }
}

function p_busers($data, $smrycond_admin) {
  global $smarty;
  
  $cond = NULL;
  
  $menu = new stdClass();
  $menu->a = 3;
  $menu->b = 1;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), '*', 'pz_user', NULL, $cond, NULL, NULL, NULL, NULL);
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_admin);
  $smarty->assign('smry', json_encode($result));
}

function p_buser($data, $smrycond_admin) {
  global $smarty;
  
  $menu = new stdClass();
  $menu->a = 3;
  $menu->b = 1;
  $smarty->assign('menu', $menu);
  
  $i = MiscUtils::getParam('i', NULL);
  if ($i) {
    $data['0'] = MiscUtils::db(getMyPdo(), '*', 'pz_user', NULL, "t.id = $i", NULL, NULL, NULL, NULL);
  }
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_admin);
  $smarty->assign('smry', json_encode($result));
}


function p_bmodels($data, $smrycond_admin) {
  global $smarty;
  
  $cond = NULL;
  
  $menu = new stdClass();
  $menu->a = 3;
  $menu->b = 2;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), '*', 'pz_model', NULL, $cond, NULL, NULL, NULL, NULL);
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_admin);
  $smarty->assign('smry', json_encode($result));
}

function p_bmodel($data, $smrycond_admin) {
  global $smarty;
  
  $cond = NULL;
  
  $menu = new stdClass();
  $menu->a = 3;
  $menu->b = 2;
  $smarty->assign('menu', $menu);
  
  $i = MiscUtils::getParam('i', NULL);
  if ($i) {
    $data['0'] = MiscUtils::db(getMyPdo(), '*', 'pz_model', NULL, "t.id = $i", NULL, NULL, NULL, NULL);
    $data['1'] = MiscUtils::db(getMyPdo(), '*', 'pz_model_state', NULL, "t.model_id = $i", NULL, NULL, NULL, NULL);
    $data['2'] = MiscUtils::db(getMyPdo(), '*', 'pz_model_transition', NULL, "t.model_id = $i", NULL, NULL, NULL, NULL);
  }
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_admin);
  $smarty->assign('smry', json_encode($result));
}


function p_padminall($data, $task_select, $task_join, $select, $join, $smrycond_admin) {
  global $smarty;
  
  $cond = $smrycond_admin;
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 1;
  $menu->c = 1;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_admin);
  $smarty->assign('smry', json_encode($result));
}

function p_padminconfirmphoto($data, $task_select, $task_join, $select, $join, $smrycond_admin) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND (t.state_id = 6)";
  
  $page = MiscUtils::getParam('p', START);

  $menu = new stdClass();
  $menu->a = 1;
  $menu->c = 2;
  
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_admin);
  $smarty->assign('smry', json_encode($result));
}

function p_padminnoconfirmphoto($data, $task_select, $task_join, $select, $join, $smrycond_admin) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND (t.state_id = 7)";
  
  $page = MiscUtils::getParam('p', START);

  $menu = new stdClass();
  $menu->a = 1;
  $menu->c = 13;
  
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_admin);
  $smarty->assign('smry', json_encode($result));
}

function p_padminconfirmsort($data, $task_select, $task_join, $select, $join, $smrycond_admin) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND t.state_id = 10";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 1;
  $menu->c = 3;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_admin);
  $smarty->assign('smry', json_encode($result));
}

function p_padminconfirmpublish($data, $task_select, $task_join, $select, $join, $smrycond_admin) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND t.state_id = 14";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 1;
  $menu->c = 4;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_admin);
  $smarty->assign('smry', json_encode($result));
}

function p_padmindesignertask($data, $task_select, $task_join, $select, $join, $smrycond_admin) {
  global $smarty;
  
  $cond = "(t.is_canceled = 0 AND t.state_id = 9)";
  
  $menu = new stdClass();
  $menu->a = 4;
  $menu->b = 1;
  $smarty->assign('menu', $menu);

  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', START, SIZE);
  $data['note'] = getNote($cond);
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_admin);
  $smarty->assign('smry', json_encode($result));
}

function p_padmindesignertaskundo($data, $task_select, $task_join, $select, $join, $smrycond_admin) {
  global $smarty;
  
  $cond = "(t.is_canceled = 0 AND (t.state_id > 9 AND t.state_id <=15 ))";
  
  $menu = new stdClass();
  $menu->a = 4;
  $menu->b = 2;
  $smarty->assign('menu', $menu);

  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', START, SIZE);
//  $data['note'] = getNote($cond);
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_admin);
  $smarty->assign('smry', json_encode($result));
}

function p_padminproject($data, $smrycond_admin) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND t.model_id = 1 AND t.prearrange_date != 0 AND t.is_asst_published = 0";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 7;
  $smarty->assign('menu', $menu);
  
  $table_select = "t.*";
  $data['0'] = MiscUtils::db(getMyPdo(), $table_select, 'pz_task', NULL, $cond, 't.prearrange_date', 'ASC', $page, INFINITE);
  $data['note'] = getNote2($cond);
  $smarty->assign('data', json_encode($data));
  
  
  $result = smry($smrycond_admin);
  $smarty->assign('smry', json_encode($result));
}

function p_padminsheet($data, $task_select, $task_join, $select, $join, $smrycond_admin) {
  global $smarty;
  
  $cond = 't.model_id = 1';
  
  $menu = new stdClass();
  $menu->a = 5;
  $smarty->assign('menu', $menu);

  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', START, SIZE);
  $data['note'] = getNote($cond);

  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_admin);
  $smarty->assign('smry', json_encode($result));
}

function p_padminscoresheet($data, $smrycond_admin) {
  global $smarty;
 
  $cond = 't.is_canceled = 0 AND t.model_id = 1 AND t.score != 0';
  
  $task_select = "t.*, 
                  t1.name AS photographer,
                  t2.name AS photographer2";
                  
  
  $task_join = array(array('pz_user', 't.photographer_id = t1.id'),
                     array('pz_user', 't.photographer2_id = t2.id')
                    );
  
  $menu = new stdClass();
  $menu->a = 6;
  $smarty->assign('menu', $menu);

  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', START, INFINITE);
  
  $photographers = array();
  for ($i = 0, $il = count($data['0']->data); $i < $il; $i++) {
    $task = $data['0']->data[$i];
    $exist1 = false; $exist2 = false; $count1 = 1; $count2 = 1; $c1 = 0; $c2 = 0;
    for ($j = 0, $jl = count($photographers); $j < $jl; $j++) {
      $tmp = $photographers[$j];
      if ($tmp->name == $task->photographer) {
        $tmp->score += $task->score;
        $tmp->count++;
        if ($task->is_admin_retouched == 1) {
          $tmp->scount++;
        }
        $exist1 = true;
      }
      if ($tmp->name == $task->photographer2) {
        $tmp->score += $task->score;
        $tmp->count++;
        if ($task->is_admin_retouched == 1) {
          $tmp->scount++;
        }
        $exist2 = true;
      }
    }
    if (!$exist1 && $task->photographer != '') {
      $m = new stdClass();
      $m->name = $task->photographer;
      $m->score += $task->score;
      $m->count = $count1;
      if ($task->is_admin_retouched == 1) {
        $c1++;
      }
      $m->scount += $c1;
      array_push($photographers, $m);
    }
    if (!$exist2 && $task->photographer2 != '') {
      $m1 = new stdClass();
      $m1->name = $task->photographer2;
      $m1->score += $task->score;
      $m1->count = $count2;
      if ($task->is_admin_retouched == 1) {
        $c2++;
      }
      $m1->scount += $c2;
      array_push($photographers, $m1);
    }
  }
  
  $data['tasks'] = $photographers;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_admin);
  $smarty->assign('smry', json_encode($result));
}

function p_padminscoresheetdetail($data, $smrycond_admin) {
  global $smarty;
 
  $options = json_decode(MiscUtils::getParam('opt', NULL));

  $cond = "t.is_canceled = 0 AND t.model_id = 1 AND t.score != 0 AND (t1.name = '$options->name' OR t2.name = '$options->name') AND $options->cond";

  $task_select = "t.customer_id, t.wedding_date, t.score, t.score_detail, t.culture1, t.culture2, t.is_admin_retouched,
                  t1.name AS photographer,
                  t2.name AS photographer2";
  
  $task_join = array(array('pz_user', 't.photographer_id = t1.id'),
                     array('pz_user', 't.photographer2_id = t2.id')
                    );
                    
  $menu = new stdClass();
  $menu->a = 6;
  $smarty->assign('menu', $menu);

  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', START, INFINITE);

  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_admin);
  $smarty->assign('smry', json_encode($result));
}

function p_padminmailall($data, $task_select, $task_join, $select, $join, $smrycond_admin) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND t.model_id = 2 AND t.state_id = 16";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 1;
  $menu->c = 5;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_admin);
  $smarty->assign('smry', json_encode($result));
}


function p_padminpersonal($data, $task_select, $task_join, $select, $join, $smrycond_admin) {
  global $smarty;
  
  $cond = "(t.is_canceled = 0 AND (((t.state_id = 19 OR t.state_id = 21) AND t.custom_assigner_id = " . $_SESSION[USER]->id . ") 
                                OR ((t.state_id = 18 OR t.state_id = 20) AND t.custom_assignee_id =  " . $_SESSION[USER]->id . ")))";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 1;
  $menu->c = 6;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_admin);
  $smarty->assign('smry', json_encode($result));
}

function p_ausers($data, $smrycond_asst) {
  global $smarty;
  
  $cond = NULL;
  
  $menu = new stdClass();
  $menu->a = 4;
  $menu->b = 1;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), '*', 'pz_user', NULL, $cond, NULL, NULL, NULL, NULL);
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}

function p_auser($data, $smrycond_asst) {
  global $smarty;
  
  $menu = new stdClass();
  $menu->a = 4;
  $menu->b = 1;
  $smarty->assign('menu', $menu);
  
  $i = MiscUtils::getParam('i', NULL);
  if ($i) {
    $data['0'] = MiscUtils::db(getMyPdo(), '*', 'pz_user', NULL, "t.id = $i", NULL, NULL, NULL, NULL);
  }
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}

function p_passistantall($data, $task_select, $task_join, $select, $join, $smrycond_asst) {
  global $smarty;
  
  $cond = $smrycond_asst;
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 1;
  $menu->c = 1;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}

function p_passistantbackup($data, $task_select, $task_join, $select, $join, $smrycond_asst) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND t.state_id = 2";
  
  $page = MiscUtils::getParam('p', START);

  $menu = new stdClass();
  $menu->a = 1;
  $menu->c = 2;

  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}


function p_passistantcheck($data, $task_select, $task_join, $select, $join, $smrycond_asst) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND (t.state_id = 3 OR t.state_id = 5)";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 1;
  $menu->c = 3;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}

function p_passistantnocheck($data, $task_select, $task_join, $select, $join, $smrycond_asst) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND (t.state_id = 4)";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 1;
  $menu->c = 10;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}

function p_passistantpickup($data, $task_select, $task_join, $select, $join, $smrycond_asst) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND t.state_id = 8";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 1;
  $menu->c = 4;
  
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}

function p_passistantauthor($data, $task_select, $task_join, $select, $join, $smrycond_asst) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND (t.state_id = 11)";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 1;
  $menu->c = 5;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}

function p_passistantrauthor($data, $task_select, $task_join, $select, $join, $smrycond_asst) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND (t.state_id = 12)";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 1;
  $menu->c = 8;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}


function p_passistantpublish($data, $task_select, $task_join, $select, $join, $smrycond_asst) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND (t.state_id = 13)";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 1;
  $menu->c = 9;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}

function p_passistantproject($data, $smrycond_asst) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND t.model_id = 1 AND t.prearrange_date != 0 AND t.is_asst_published = 0";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 3;
  $smarty->assign('menu', $menu);
  
  $table_select = "t.customer_id, t.prearrange_date, t.branch, t.ub_akl, t.ub_syd, t.ub_mel, t.ub_nyc, t.ub_bne, ub_xtrao";
  $data['0'] = MiscUtils::db(getMyPdo(), $table_select, 'pz_task', NULL, $cond, 't.prearrange_date', 'ASC', $page, INFINITE);
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}

function p_passistantmailall($data, $task_select, $task_join, $select, $join, $smrycond_asst) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND t.model_id = 2 AND t.state_id = 16";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 1;
  $menu->c = 6;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}

function p_passistantpersonal($data, $task_select, $task_join, $select, $join, $smrycond_asst) {
  global $smarty;
  
  $cond = "(t.is_canceled = 0 AND (((t.state_id = 19 OR t.state_id = 21) AND t.custom_assigner_id = " . $_SESSION[USER]->id . ") 
                                OR ((t.state_id = 18 OR t.state_id = 20) AND t.custom_assignee_id =  " . $_SESSION[USER]->id . ")))";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 1;
  $menu->c = 7;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}

function receivemail($data, $smrycond_asst, $smrycond_admin) {
  global $smarty;
  
  $id = MiscUtils::getParam('i', NULL);
  
  if ($id) {
    $sql = "SELECT t.* FROM pz_task AS t WHERE t.id = $id";
    $stmt = getMyPdo()->prepare($sql);
    $stmt->execute();
    $res = array();
    $i = 0;
    while ($i < $stmt->rowCount()) {
      $tmp = $stmt->fetch(PDO::FETCH_OBJ);
      $sql2 = "SELECT * FROM pz_task AS tk WHERE tk.parent_trackId = '$tmp->track_id'";
      $stmt2 = getMyPdo()->prepare($sql2);
      $stmt2->execute();
      $tmp->tasks = array();
      $j = 0;
      while ($j < $stmt2->rowCount()) {
        $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
        $tmp->tasks[] = $tmp2;
        $j++;
      }
      $res[] = $tmp;
      $i++;
    }
  }
  $data['0'] = $res;
  $smarty->assign('data', json_encode($data));
  
  $menu = new stdClass();
  $menu->a = 1;
  $smarty->assign('menu', $menu);
  
  $result = ($_SESSION['user']->type == USER_TYPE_ADMINISTER) ? smry($smrycond_admin) : smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}


function p_passistantsheet($data, $task_select, $task_join, $select, $join, $smrycond_asst) {
  global $smarty;
  
  $cond = 't.model_id = 1';
  
  $menu = new stdClass();
  $menu->a = 5;
  $smarty->assign('menu', $menu);

  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', START, SIZE);
  $data['note'] = getNote($cond);

  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}

function p_passistantscoresheet($data, $smrycond_asst) {
  global $smarty;
 
  $cond = 't.is_canceled = 0 AND t.model_id = 1 AND t.score != 0';
  
  $task_select = "t.*, 
                  t1.name AS photographer,
                  t2.name AS photographer2";
                  
  
  $task_join = array(array('pz_user', 't.photographer_id = t1.id'),
                     array('pz_user', 't.photographer2_id = t2.id')
                    );
  
  $menu = new stdClass();
  $menu->a = 6;
  $smarty->assign('menu', $menu);

  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', START, INFINITE);
  
  $photographers = array();
  for ($i = 0, $il = count($data['0']->data); $i < $il; $i++) {
    $task = $data['0']->data[$i];
    $exist1 = false; $exist2 = false; $count1 = 1; $count2 = 1; $c1 = 0; $c2 = 0;
    for ($j = 0, $jl = count($photographers); $j < $jl; $j++) {
      $tmp = $photographers[$j];
      if ($tmp->name == $task->photographer) {
        $tmp->score += $task->score;
        $tmp->count++;
        if ($task->is_admin_retouched == 1) {
          $tmp->scount++;
        }
        $exist1 = true;
      }
      if ($tmp->name == $task->photographer2) {
        $tmp->score += $task->score;
        $tmp->count++;
        if ($task->is_admin_retouched == 1) {
          $tmp->scount++;
        }
        $exist2 = true;
      }
    }
    if (!$exist1 && $task->photographer != '') {
      $m = new stdClass();
      $m->name = $task->photographer;
      $m->score += $task->score;
      $m->count = $count1;
      if ($task->is_admin_retouched == 1) {
        $c1++;
      }
      $m->scount += $c1;
      array_push($photographers, $m);
    }
    if (!$exist2 && $task->photographer2 != '') {
      $m1 = new stdClass();
      $m1->name = $task->photographer2;
      $m1->score += $task->score;
      $m1->count = $count2;
      if ($task->is_admin_retouched == 1) {
        $c2++;
      }
      $m1->scount += $c2;
      array_push($photographers, $m1);
    }
  }
  
  $data['tasks'] = $photographers;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}

function p_passistantscoresheetdetail($data, $smrycond_asst) {
  global $smarty;
 
  $options = json_decode(MiscUtils::getParam('opt', NULL));

  $cond = "t.is_canceled = 0 AND t.model_id = 1 AND t.score != 0 AND (t1.name = '$options->name' OR t2.name = '$options->name') AND $options->cond";

  $task_select = "t.customer_id, t.wedding_date, t.score, t.score_detail, t.culture1, t.culture2, t.is_admin_retouched,
                  t1.name AS photographer,
                  t2.name AS photographer2";
  
  $task_join = array(array('pz_user', 't.photographer_id = t1.id'),
                     array('pz_user', 't.photographer2_id = t2.id')
                    );
                    
  $menu = new stdClass();
  $menu->a = 6;
  $smarty->assign('menu', $menu);

  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', START, INFINITE);

  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}


function p_pdesignerspreadsheet($data, $task_select, $task_join, $select, $join, $smrycond_des) {
  global $smarty;
  
  $cond = "((t.designer_id = " . $_SESSION[USER]->id . ") OR ((t.custom_assigner_id = " . $_SESSION[USER]->id . ") OR (t.custom_assignee_id =  " . $_SESSION[USER]->id . ")))";
  
  
  $menu = new stdClass();
  $menu->a = 2;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', START, SIZE);
  $data['note'] = getNote($cond);
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_des);
  $smarty->assign('smry', json_encode($result));
}

function p_pdesignerall($data, $task_select, $task_join, $select, $join, $smrycond_des) {
  global $smarty;
  
  $cond = $smrycond_des;
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 1;
  $menu->c = 1;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_des);
  $smarty->assign('smry', json_encode($result));
}

function p_pdesignercomplete($data, $task_select, $task_join, $select, $join, $smrycond_des) {
  global $smarty;
  
  $cond = "(t.is_canceled = 0 AND (t.state_id = 9 AND t.designer_id = " . $_SESSION[USER]->id . "))";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 1;
  $menu->c = 2;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_des);
  $smarty->assign('smry', json_encode($result));
}


function p_pdesignerpersonal($data, $task_select, $task_join, $select, $join, $smrycond_des) {
  global $smarty;
  
  $cond = "(t.is_canceled = 0 AND (((t.state_id = 19 OR t.state_id = 21 ) AND t.custom_assigner_id = " . $_SESSION[USER]->id . ")
                                OR ((t.state_id = 18 OR t.state_id = 20) AND t.custom_assignee_id =  " . $_SESSION[USER]->id . ")))";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 1;
  $menu->c = 3;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_des);
  $smarty->assign('smry', json_encode($result));
}


function reviewmail($data, $smrycond_admin, $smrycond_asst) {
  global $smarty;
  
  $id = MiscUtils::getParam('i', NULL);
  
  if ($id) {
    $sql = "SELECT t.* FROM pz_task AS t WHERE t.id = $id";
    $stmt = getMyPdo()->prepare($sql);
    $stmt->execute();
    $res = array();
    $i = 0;
    while ($i < $stmt->rowCount()) {
      $tmp = $stmt->fetch(PDO::FETCH_OBJ);
      $sql2 = "SELECT * FROM pz_task AS tk WHERE tk.parent_trackId = '$tmp->track_id'";
      $stmt2 = getMyPdo()->prepare($sql2);
      $stmt2->execute();
      $tmp->tasks = array();
      $j = 0;
      while ($j < $stmt2->rowCount()) {
        $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
        $tmp->tasks[] = $tmp2;
        $j++;
      }
      $res[] = $tmp;
      $i++;
    }
  }
  $data['0'] = $res;
  $smarty->assign('data', json_encode($data));
  
  $menu = new stdClass();
  $menu->a = ($_SESSION['user']->type == USER_TYPE_ADMINISTER || $_SESSION['user']->type == USER_TYPE_MAILER ) ? 2 : 3;
  $smarty->assign('menu', $menu);
  if ($_SESSION['user']->type != USER_TYPE_MAILER) {
    $result = ($_SESSION['user']->type == USER_TYPE_ADMINISTER) ? smry($smrycond_admin) : smry($smrycond_asst);
    $smarty->assign('smry', json_encode($result));
  }
}

function p_pmaileraddparcel($data) {
  global $smarty;
  
  $data['courier'] = MiscUtils::db(getMyPdo(), '*', 'pz_courier', NULL, NULL, NULL, NULL, NULL, NULL);
  $smarty->assign('data', json_encode($data));
  
  $menu = new stdClass();
  $menu->a = 1;
  $smarty->assign('menu', $menu);
}

function p_bcouriers($data) {
  global $smarty;
  
  $cond = NULL;
  
  $menu = new stdClass();
  $menu->a = 4;
  $menu->b = 1;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), '*', 'pz_courier', NULL, $cond, NULL, NULL, NULL, NULL);
  $smarty->assign('data', json_encode($data));
}

function p_bcourier($data) {
  global $smarty;
  
  $menu = new stdClass();
  $menu->a = 4;
  $menu->b = 1;
  $smarty->assign('menu', $menu);
  
  $i = MiscUtils::getParam('i', NULL);
  if ($i) {
    $data['0'] = MiscUtils::db(getMyPdo(), '*', 'pz_courier', NULL, "t.id = $i", NULL, NULL, NULL, NULL);
  }
  $smarty->assign('data', json_encode($data));
}

function p_bcultures($data) {
  global $smarty;
  
  $cond = NULL;
  
  $menu = new stdClass();
  $menu->a = 4;
  $menu->b = 2;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), '*', 'pz_culture', NULL, $cond, NULL, NULL, NULL, NULL);
  $smarty->assign('data', json_encode($data));
}

function p_bculture($data) {
  global $smarty;
  
  $menu = new stdClass();
  $menu->a = 4;
  $menu->b = 2;
  $smarty->assign('menu', $menu);
  
  $i = MiscUtils::getParam('i', NULL);
  if ($i) {
    $data['0'] = MiscUtils::db(getMyPdo(), '*', 'pz_culture', NULL, "t.id = $i", NULL, NULL, NULL, NULL);
  }
  $smarty->assign('data', json_encode($data));
}

function p_bceremonys($data) {
  global $smarty;
  
  $cond = NULL;
  
  $menu = new stdClass();
  $menu->a = 4;
  $menu->b = 3;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), '*', 'pz_ceremony', NULL, $cond, NULL, NULL, NULL, NULL);
  $smarty->assign('data', json_encode($data));
}

function p_bceremony($data) {
  global $smarty;
  
  $menu = new stdClass();
  $menu->a = 4;
  $menu->b = 3;
  $smarty->assign('menu', $menu);
  
  $i = MiscUtils::getParam('i', NULL);
  if ($i) {
    $data['0'] = MiscUtils::db(getMyPdo(), '*', 'pz_ceremony', NULL, "t.id = $i", NULL, NULL, NULL, NULL);
  }
  $smarty->assign('data', json_encode($data));
}

function p_breceptions($data) {
  global $smarty;
  
  $cond = NULL;
  
  $menu = new stdClass();
  $menu->a = 4;
  $menu->b = 4;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), '*', 'pz_reception', NULL, $cond, NULL, NULL, NULL, NULL);
  $smarty->assign('data', json_encode($data));
}

function p_breception($data) {
  global $smarty;
  
  $menu = new stdClass();
  $menu->a = 4;
  $menu->b = 4;
  $smarty->assign('menu', $menu);
  
  $i = MiscUtils::getParam('i', NULL);
  if ($i) {
    $data['0'] = MiscUtils::db(getMyPdo(), '*', 'pz_reception', NULL, "t.id = $i", NULL, NULL, NULL, NULL);
  }
  $smarty->assign('data', json_encode($data));
}

function p_bweddingcolors($data) {
  global $smarty;
  
  $cond = NULL;
  
  $menu = new stdClass();
  $menu->a = 4;
  $menu->b = 5;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), '*', 'pz_color', NULL, $cond, NULL, NULL, NULL, NULL);
  $smarty->assign('data', json_encode($data));
}

function p_bweddingcolor($data) {
  global $smarty;
  
  $menu = new stdClass();
  $menu->a = 4;
  $menu->b = 5;
  $smarty->assign('menu', $menu);
  
  $i = MiscUtils::getParam('i', NULL);
  if ($i) {
    $data['0'] = MiscUtils::db(getMyPdo(), '*', 'pz_color', NULL, "t.id = $i", NULL, NULL, NULL, NULL);
  }
  $smarty->assign('data', json_encode($data));
}
function p_pmailerallparcel($data, $task_select, $task_join, $select, $join) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND t.model_id = 2 AND (t.state_id = 16 OR t.state_id = 17)";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 2;
  $menu->c = 1;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
}

function p_pmailerunreceivedparcel($data, $task_select, $task_join, $select, $join) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND t.model_id = 2 AND (t.state_id = 16)";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 2;
  $menu->c = 2;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
}

function p_pmailerreceivedparcel($data, $task_select, $task_join, $select, $join) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND t.model_id = 2 AND (t.state_id = 17)";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 2;
  $menu->c = 3;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
}

function p_pmailerunreceivedcustomer($data, $task_select, $task_join, $select, $join) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND t.model_id = 1 AND (t.state_id = 1)";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 3;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
}


function p_pmailersheet($data, $task_select, $task_join, $select, $join) {
  global $smarty;
  
  $cond = 't.model_id = 1';
  
  $menu = new stdClass();
  $menu->a = 5;
  $smarty->assign('menu', $menu);

  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', START, SIZE);
  $data['note'] = getNote($cond);

  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
}

function p_pmailerscoresheet($data) {
  global $smarty;
 
  $cond = 't.is_canceled = 0 AND t.model_id = 1 AND t.score != 0';
  
  $task_select = "t.*, 
                  t1.name AS photographer,
                  t2.name AS photographer2";
                  
  
  $task_join = array(array('pz_user', 't.photographer_id = t1.id'),
                     array('pz_user', 't.photographer2_id = t2.id')
                    );
  
  $menu = new stdClass();
  $menu->a = 6;
  $smarty->assign('menu', $menu);

  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', START, INFINITE);
  
  $photographers = array();
  for ($i = 0, $il = count($data['0']->data); $i < $il; $i++) {
    $task = $data['0']->data[$i];
    $exist1 = false; $exist2 = false; $count1 = 1; $count2 = 1; $c1 = 0; $c2 = 0;
    for ($j = 0, $jl = count($photographers); $j < $jl; $j++) {
      $tmp = $photographers[$j];
      if ($tmp->name == $task->photographer) {
        $tmp->score += $task->score;
        $tmp->count++;
        if ($task->is_admin_retouched == 1) {
          $tmp->scount++;
        }
        $exist1 = true;
      }
      if ($tmp->name == $task->photographer2) {
        $tmp->score += $task->score;
        $tmp->count++;
        if ($task->is_admin_retouched == 1) {
          $tmp->scount++;
        }
        $exist2 = true;
      }
    }
    if (!$exist1 && $task->photographer != '') {
      $m = new stdClass();
      $m->name = $task->photographer;
      $m->score += $task->score;
      $m->count = $count1;
      if ($task->is_admin_retouched == 1) {
        $c1++;
      }
      $m->scount += $c1;
      array_push($photographers, $m);
    }
    if (!$exist2 && $task->photographer2 != '') {
      $m1 = new stdClass();
      $m1->name = $task->photographer2;
      $m1->score += $task->score;
      $m1->count = $count2;
      if ($task->is_admin_retouched == 1) {
        $c2++;
      }
      $m1->scount += $c2;
      array_push($photographers, $m1);
    }
  }
  
  $data['tasks'] = $photographers;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
}

function p_pmailerscoresheetdetail($data) {
  global $smarty;
 
  $options = json_decode(MiscUtils::getParam('opt', NULL));

  $cond = "t.is_canceled = 0 AND t.model_id = 1 AND t.score != 0 AND (t1.name = '$options->name' OR t2.name = '$options->name') AND $options->cond";

  $task_select = "t.customer_id, t.wedding_date, t.score, t.score_detail, t.culture1, t.culture2, t.is_admin_retouched,
                  t1.name AS photographer,
                  t2.name AS photographer2";
  
  $task_join = array(array('pz_user', 't.photographer_id = t1.id'),
                     array('pz_user', 't.photographer2_id = t2.id')
                    );
                    
  $menu = new stdClass();
  $menu->a = 6;
  $smarty->assign('menu', $menu);

  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', START, INFINITE);

  $smarty->assign('data', json_encode($data));
}


function plastupdated($data) {
  global $smarty;
  $smarty->assign('data', json_encode($data));
}

/////////////////////////////////

function p_padminspreadsheet($data, $task_select, $task_join, $select, $join, $smrycond_admin) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND ((t.state_id != 1) OR ((t.custom_assigner_id = " . $_SESSION[USER]->id . ") OR (t.custom_assignee_id =  " . $_SESSION[USER]->id . ")))";
  
  $menu = new stdClass();
  $menu->a = 2;
  $menu->c = 1;
  $smarty->assign('menu', $menu);

  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', START, SIZE);
  $data['note'] = getNote($cond);
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_admin);
  $smarty->assign('smry', json_encode($result));
}


function p_passistantspreadsheet($data, $task_select, $task_join, $select, $join, $smrycond_asst) {
  global $smarty;
  $cond = "t.is_canceled = 0 AND ((t.state_id != 1) OR ((t.custom_assigner_id = " . $_SESSION[USER]->id . ") OR (t.custom_assignee_id =  " . $_SESSION[USER]->id . ")))";
  
  $menu = new stdClass();
  $menu->a = 2;
  $menu->c = 1;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', START, SIZE);
  $data['note'] = getNote($cond);
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}

function p_cpassistantbackup($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND t.state_id = 2";
  
  $page = MiscUtils::getParam('p', START);

  $menu = new stdClass();
  $menu->a = 2;
  $menu->c = 2;

  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = ($_SESSION[USER]->type == USER_TYPE_ADMINISTER) ? smry($smrycond_admin) : smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}


function p_cpassistantcheck($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND (t.state_id = 3 OR t.state_id = 5)";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 2;
  $menu->c = 3;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = ($_SESSION[USER]->type == USER_TYPE_ADMINISTER) ? smry($smrycond_admin) : smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}

function p_cpassistantnocheck($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND (t.state_id = 4)";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 2;
  $menu->c = 16;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = ($_SESSION[USER]->type == USER_TYPE_ADMINISTER) ? smry($smrycond_admin) : smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}


function p_cpadminconfirmphoto($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND (t.state_id = 6)";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 2;
  $menu->c = 4;

  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = ($_SESSION[USER]->type == USER_TYPE_ADMINISTER) ? smry($smrycond_admin) : smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}

function p_cpadminnoconfirmphoto($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND (t.state_id = 7)";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 2;
  $menu->c = 17;

  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = ($_SESSION[USER]->type == USER_TYPE_ADMINISTER) ? smry($smrycond_admin) : smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}

function p_cpassistantpickup($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND t.state_id = 8";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 2;
  $menu->c = 5;
  
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = ($_SESSION[USER]->type == USER_TYPE_ADMINISTER) ? smry($smrycond_admin) : smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}


function p_cpdesignercomplete($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin) {
  global $smarty;
  
  $cond = "(t.is_canceled = 0 AND (t.state_id = 9))";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 2;
  $menu->c = 6;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = ($_SESSION[USER]->type == USER_TYPE_ADMINISTER) ? smry($smrycond_admin) : smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}

function p_cpadminconfirmsort($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND t.state_id = 10";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 2;
  $menu->c = 7;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = ($_SESSION[USER]->type == USER_TYPE_ADMINISTER) ? smry($smrycond_admin) : smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}

function p_cpassistantauthor($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND (t.state_id = 11)";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 2;
  $menu->c = 8;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = ($_SESSION[USER]->type == USER_TYPE_ADMINISTER) ? smry($smrycond_admin) : smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}

function p_cpassistantrauthor($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND (t.state_id = 12)";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 2;
  $menu->c = 9;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = ($_SESSION[USER]->type == USER_TYPE_ADMINISTER) ? smry($smrycond_admin) : smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}


function p_cpassistantpublish($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND (t.state_id = 13)";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 2;
  $menu->c = 10;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = ($_SESSION[USER]->type == USER_TYPE_ADMINISTER) ? smry($smrycond_admin) : smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}

function p_cpadminconfirmpublish($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND t.state_id = 14";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 2;
  $menu->c = 11;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = ($_SESSION[USER]->type == USER_TYPE_ADMINISTER) ? smry($smrycond_admin) : smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}

function p_cpadminconfirmpublished($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND t.state_id = 15";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 2;
  $menu->c = 12;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = ($_SESSION[USER]->type == USER_TYPE_ADMINISTER) ? smry($smrycond_admin) : smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}

function p_cpmailerunreceivedparcel($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND t.model_id = 2 AND (t.state_id = 16)";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 2;
  $menu->c = 13;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = ($_SESSION[USER]->type == USER_TYPE_ADMINISTER) ? smry($smrycond_admin) : smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}

function p_cpmailerreceivedparcel($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin) {
  global $smarty;
  
  $cond = "t.is_canceled = 0 AND t.model_id = 2 AND (t.state_id = 17)";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 2;
  $menu->c = 14;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = ($_SESSION[USER]->type == USER_TYPE_ADMINISTER) ? smry($smrycond_admin) : smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}


function p_cpadminpersonal($data, $task_select, $task_join, $select, $join, $smrycond_asst, $smrycond_admin) {
  global $smarty;
  
  $cond = "(t.is_canceled = 0 AND (((t.state_id = 19 OR t.state_id = 21) AND t.custom_assigner_id = " . $_SESSION[USER]->id . ") 
                                OR ((t.state_id = 18 OR t.state_id = 20) AND t.custom_assignee_id =  " . $_SESSION[USER]->id . ")))";
  
  $page = MiscUtils::getParam('p', START);
  
  $menu = new stdClass();
  $menu->a = 2;
  $menu->c = 15;
  $smarty->assign('menu', $menu);
  
  $data['0'] = MiscUtils::db(getMyPdo(), $task_select, 'pz_task', $task_join, $cond, 't.deadline', 'ASC', $page, SIZE);
  $data['note'] = getNote($cond);
  
  $condition = "t.user_id = " . $_SESSION[USER]->id;
  $data['event'] = MiscUtils::db(getMyPdo(), $select, 'pz_task_event', $join, $condition, 't.id', 'DESC', START, SIZE);
  $data['event_cond'] = $condition;
  $data['event_select'] = $select;
  $data['event_join'] = $join;
  
  $data['cond'] = $cond;
  $data['table_select'] = $task_select;
  $data['table_join'] = $task_join;
  $smarty->assign('data', json_encode($data));
  
  $result = ($_SESSION[USER]->type == USER_TYPE_ADMINISTER) ? smry($smrycond_admin) : smry($smrycond_asst);
  $smarty->assign('smry', json_encode($result));
}


function getMyPdo() {
  try {
    $host = DB_HOST;
    $login = DB_LOGIN;
    $pass = DB_PASS;
    $db = DB_NAME;
    $myPdo = new PDO ("mysql:host=$host", $login, $pass);
    $myPdo->exec("USE $db");
    $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    return $myPdo;
  } catch (PDOException $e) {
    echo $e->getMessage ();
  }
  return NULL;
}

function getNote($condition) {
  global $smarty;
  if ($condition == NULL) {
    $condition = '1=1';
  }
  
  $sql = "SELECT t.id FROM pz_task AS t WHERE $condition";
  $stmt = getMyPdo()->prepare($sql);
  $stmt->execute();
  $result = array();
  $i = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $sql2 = "SELECT COUNT(n.id) AS total FROM pz_task_note AS n WHERE n.task_id = $tmp->id AND n.note_is_canceled = 0";
    $stmt2 = getMyPdo()->prepare($sql2);
    $stmt2->execute();
    $tmp->value = 0;
    if ($stmt2->rowCount() == 1) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      $tmp->value = $tmp2->total;
    }
    $result[] = $tmp;
    $i++;
  }
  return $result;
}


function getNote2($condition) {
  global $smarty;
  if ($condition == NULL) {
    $condition = '1=1';
  }
  
  $sql = "SELECT t.id FROM pz_task AS t WHERE $condition";
  $stmt = getMyPdo()->prepare($sql);
  $stmt->execute();
  $result = array();
  $i = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $sql2 = "SELECT * FROM pz_task_note AS n WHERE n.task_id = $tmp->id AND n.note_is_canceled = 0 AND n.is_planning = 1";
    $stmt2 = getMyPdo()->prepare($sql2);
    $stmt2->execute();
    $tmp->notes = array();
    if ($stmt2->rowCount() == 1) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      $tmp->notes[] = $tmp2;
    }
    $result[] = $tmp;
    $i++;
  }
  return $result;
}

function smry($condition) {
  global $smarty;
  $cond = $condition . ' AND t.deadline < ' . time() . ' AND t.is_completed = 0';
  
  $result = array();
  $sql = "SELECT COUNT(t.id) as total FROM pz_task AS t WHERE $condition";
  $stmt = getMyPdo()->prepare($sql);
  $stmt->execute();
  $tmp = $stmt->fetch(PDO::FETCH_OBJ);
  $result['total'] = $tmp->total;
  
  $sql1 = "SELECT COUNT(t.id) as total FROM pz_task AS t WHERE $cond";
  $stmt1 = getMyPdo()->prepare($sql1);
  $stmt1->execute();
  $tmp1 = $stmt1->fetch(PDO::FETCH_OBJ);
  $result['urgent'] = $tmp1->total;
  
  return $result;
}
?>
